package com.example.city_home_app.RecyclerViewAdapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.city_home_app.Activity.MomentAddActivity;
import com.example.city_home_app.R;
import com.example.city_home_app.UI.UIUtils;

import java.io.IOException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Adapter_MomentAdd extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Uri> list;
    private Context context;
    private int type;

    public Adapter_MomentAdd(Context context) {
        this.context = context;
    }

    public void setList(List<Uri> list) {
        this.list = list;
    }

    public void settype(int type){
        this.type = type;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.recitem_postimage, parent, false);
        Adapter_MomentAdd.ViewHolder viewHolder = new Adapter_MomentAdd.ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {

        final Adapter_MomentAdd.ViewHolder viewHolder = (Adapter_MomentAdd.ViewHolder) holder;
        try {
            if(type==1) {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), list.get(position));
                viewHolder.imgRecitemPostimgImg.setImageBitmap(bitmap);
            }else{
                MediaMetadataRetriever media = new MediaMetadataRetriever();
                media.setDataSource(UIUtils.getFilePathFromUri(context,list.get(0)));// videoPath 本地视频的路径
                Bitmap bitmap  = media.getFrameAtTime(1, MediaMetadataRetriever.OPTION_CLOSEST_SYNC );
                viewHolder.imgRecitemPostimgImg.setImageBitmap(bitmap);//对应的ImageView赋值图片
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        viewHolder.imgRecitemPostimgDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(type==1) {
                    MomentAddActivity.instance.delimg(position);
                }else{
                    MomentAddActivity.instance.delvideo();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_recitem_postimg_img)
        ImageView imgRecitemPostimgImg;
        @BindView(R.id.img_recitem_postimg_delete)
        ImageView imgRecitemPostimgDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
